package com.ruoyi.shop.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.shop.domain.vo.ShopCouponVo;
import com.ruoyi.shop.service.IShopCouponCodeService;
import com.ruoyi.shop.service.IShopCouponUsedRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.shop.mapper.ShopCouponMapper;
import com.ruoyi.shop.domain.ShopCoupon;
import com.ruoyi.shop.service.IShopCouponService;
import com.ruoyi.common.core.text.Convert;

/**
 * 优惠券Service业务层处理
 * 
 * @author Alan Zhang
 * @date 2021-09-18
 */
@Service
public class ShopCouponServiceImpl implements IShopCouponService 
{
    @Autowired
    private ShopCouponMapper shopCouponMapper;

    @Autowired
    private IShopCouponCodeService shopCouponCodeService;
    @Autowired
    private IShopCouponUsedRecordService shopCouponUsedRecordService;

    /**
     * 查询优惠券
     * 
     * @param id 优惠券主键
     * @return 优惠券
     */
    @Override
    public ShopCoupon selectShopCouponById(Long id)
    {
        return shopCouponMapper.selectShopCouponById(id);
    }

    /**
     * 查询优惠券列表
     * 
     * @param shopCoupon 优惠券
     * @return 优惠券
     */
    @Override
    public List<ShopCoupon> selectShopCouponList(ShopCoupon shopCoupon)
    {
        return shopCouponMapper.selectShopCouponList(shopCoupon);
    }

    /**
     * 新增优惠券
     * 
     * @param shopCoupon 优惠券
     * @return 结果
     */
    @Override
    public int insertShopCoupon(ShopCoupon shopCoupon)
    {
        shopCoupon.setCreateTime(DateUtils.getNowDate());
        return shopCouponMapper.insertShopCoupon(shopCoupon);
    }

    /**
     * 修改优惠券
     * 
     * @param shopCoupon 优惠券
     * @return 结果
     */
    @Override
    public int updateShopCoupon(ShopCoupon shopCoupon)
    {
        shopCoupon.setUpdateTime(DateUtils.getNowDate());
        return shopCouponMapper.updateShopCoupon(shopCoupon);
    }

    /**
     * 批量删除优惠券
     * 
     * @param ids 需要删除的优惠券主键
     * @return 结果
     */
    @Override
    public int deleteShopCouponByIds(String ids)
    {
        return shopCouponMapper.deleteShopCouponByIds(Convert.toStrArray(ids));
    }



    /**
     * 删除优惠券信息
     * 
     * @param id 优惠券主键
     * @return 结果
     */
    @Override
    public int deleteShopCouponById(Long id)
    {
        return shopCouponMapper.deleteShopCouponById(id);
    }

    /**
     * 更新发优惠券数量
     * @param couponId
     */
    @Override
    public int doSyncTakeCount(Long couponId) {
        long count = shopCouponCodeService.queryCountByCouponId(couponId);
        ShopCoupon coupon = new ShopCoupon();
        coupon.setId(couponId);
        coupon.setTakeCount(count);
        return updateShopCoupon(coupon);
    }

    /**
     * 更新优惠券使用数量
     * @param couponId
     */
    @Override
    public int doSyncUsedCount(Long couponId) {
        long count = shopCouponUsedRecordService.queryCountByCouponId(couponId);
        ShopCoupon coupon = new ShopCoupon();
        coupon.setId(couponId);
        coupon.setUsedCount(count);
        return updateShopCoupon(coupon);
    }

    /**
     * 用户领券信息
     * @param userId
     * @return
     */
    @Override
    public List<ShopCouponVo> selectJoinShopCouponByUserId(Long userId){
        return shopCouponMapper.selectJoinShopCouponByUserId(userId);
    }
}
